package com.imooc.miaosha.service;

import com.imooc.miaosha.dao.GoodsDao;
import com.imooc.miaosha.pojo.domain.Goods;
import com.imooc.miaosha.pojo.domain.MiaoshaOrder;
import com.imooc.miaosha.pojo.domain.MiaoshaUser;
import com.imooc.miaosha.pojo.domain.OrderInfo;
import com.imooc.miaosha.pojo.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class MiaoshaService {
    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    //在这里加上事务应该是比较简洁的方式了，也就是把减少库存、下订单这些操作放在一个大的事务中
    @Transactional
    public OrderInfo miaosha(MiaoshaUser user, GoodsVo goodsVo){
        //step1:减少库存.减少的是miaosha_goods表中的库存，
        // 因为毕竟是从这张表提取的秒杀信息的，goods表的库存并不是全部存在于miaosha_goods中的
        goodsService.reduceStock(goodsVo);

        //step2:下订单，分为两步骤：写订单(order_info)、写用户 订单 商品关联(miaoshao_order)

        OrderInfo orderInfo = orderService.createOrder(user,goodsVo);
        return orderInfo;

    }
}
